package com.example.mytestdemo.customviewbook.three

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Path
import android.graphics.RectF
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.widget.LinearLayout

class CustomRoundLayout : LinearLayout {
    private val mPath = Path()
    private var mBgColor: String? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onDraw(canvas: Canvas) {
        if (TextUtils.isEmpty(mBgColor)) {
            val bgDrawable = background
            if (bgDrawable is ColorDrawable) {
                val color = bgDrawable.color
                mBgColor = "#" + String.format("%08x", color)
                Log.d("qijian", "color:$color   $mBgColor")
            }
        }
//        setBackgroundColor(Color.parseColor("#00FFFFFF"))
        super.onDraw(canvas)
    }

    override fun dispatchDraw(canvas: Canvas) {
        mPath.reset()
        mPath.addRoundRect(
            RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat()),
            50f,
            50f,
            Path.Direction.CW
        )
        canvas.save()
        canvas.clipPath(mPath)
        if (!TextUtils.isEmpty(mBgColor)) {
            canvas.drawColor(Color.parseColor(mBgColor))
        }
        super.dispatchDraw(canvas)
        canvas.restore()
    }
}